﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;


namespace Chinaer.Core.Helper
{
    /// <summary>
    /// log帮助类
    /// </summary>
    public sealed class LogHelper
    {
        private static LogHelper _instance = null;
        public static LogHelper Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new LogHelper();
                }
                return _instance;
            }
        }

        private string _logFile = "common.log";
        private string _logPath = @"c:\logs\";
        public LogHelper() {
         //   _logPath = HttpRuntime.BinDirectory+"/logs/"; 
            string logPath = ConfigHelper.GetAppSetting("logPath");
            if (!string.IsNullOrEmpty(logPath))
            {
                _logPath = logPath;
            }
        }
        /// <summary>
        /// 监测日志目录是否存在
        /// </summary>
        private void CheckPath()
        {
            if (!Directory.Exists(_logPath))
            {
                try
                {
                    Directory.CreateDirectory(_logPath);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        /// <summary>
        /// 获取要写入的日志文件的路径
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private string GetPathWithFileName(string fileName)
        {
            try
            {
                CheckPath();
                return string.Format("{0}{1}", _logPath, fileName);
            }
            catch (Exception ex)
            {
                return null;
                throw new Exception(ex.Message);
            }
        }
        public void WriteSQLLog(string content)
        {
            WriteLog("SQL", "SQL", content);
        }
        

        /// <summary>
        /// 写入日志文件 默认文件名common.log remark为空
        /// </summary>
        /// <param name="content"></param>
        public void WriteLog(string content)
        {
            WriteLog(null, content);
        }
        /// <summary>
        /// 日志写入 默认文件名common.log
        /// </summary>
        /// <param name="remark"></param>
        /// <param name="content"></param>
        public void WriteLog(string remark, string content)
        {
            WriteLog(_logFile, remark, content);
        }
        /// <summary>
        /// 日志写入类
        /// </summary>
        /// <param name="logFile">如果传入null 则默认文件名为common.log</param>
        /// <param name="remark">评论</param>
        /// <param name="content">日志内容</param>
        public void WriteLog(string logFile, string remark, string content)
        {
            if (logFile.IndexOf(".") == -1)
            {
                logFile = logFile + ".txt";
            }
            StreamWriter writer = null;
            string path = GetPathWithFileName(logFile == null ? _logFile : logFile);
            StringBuilder sb = new StringBuilder();
            if (string.IsNullOrEmpty(path))
            {
                return;
            }
            try
            {
                if (File.Exists(path))
                {
                    FileInfo fileInfo = new FileInfo(path);
                    if (fileInfo.Length > 0x10000000)
                    {
                        File.Delete(path);
                        writer = File.CreateText(path);
                    }
                    else
                    {
                        writer = new StreamWriter(path, true, Encoding.UTF8);
                    }
                }
                else
                {
                    writer = new StreamWriter(path, true, Encoding.UTF8);
                }
                sb.Append("[");
                sb.Append(DateTime.Now.ToString());
                sb.Append("]");
                sb.Append("   [");
                sb.Append(remark == null ? "" : remark);
                sb.Append("]");
                sb.Append("[");
                sb.Append(content == null ? "" : content);
                sb.Append("]");
                writer.WriteLine(sb.ToString());
                writer.Close();
            }
            catch (Exception ex)
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }
    }
}
